﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NDde.Client;
using System.Windows.Forms;
using SaveCurrentState.Util;
using System.Net;
using System.Web;

namespace SaveCurrentState
{
    public class WindowExeplorerAndIEManage {
        private static WindowExeplorerAndIEManage s_instance;
        private readonly string[] browserTypes;

        public static WindowExeplorerAndIEManage GetInstance() {
            if (s_instance == null) {
                s_instance = new WindowExeplorerAndIEManage();
            }

            return s_instance;
        }

        public void GetOfficeFilePath() {
            //var doc = Globals.ThisAddIn.Application.ActiveDocument;
        }

        private WindowExeplorerAndIEManage() {
            browserTypes = new[] { "opera", "firefox","Chrome", "safari" };
        }


        public void GetCurrentLocationOfWindowExplorerAndIE() {
            SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();

            string filename;

            foreach (SHDocVw.InternetExplorer ie in shellWindows) {
                filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

                if (filename.Equals("iexplore")) {
                    Console.WriteLine(" {0}", ie.LocationURL);
                }

                if (filename.Equals("explorer")) {
                    Console.WriteLine("Hard Drive : {0}", ie.LocationURL);
                }
            }
        }

        public void GetBrowsersUrl() {
            var links = new List<string>();
            foreach (var browser in browserTypes) {
                links.Add( GetBrowserURL(browser));
            }
        }

        private string GetBrowserURL(string browser) {
            try {
                DdeClient dde = new DdeClient(browser, "WWW_GetWindowInfo");
                dde.Connect();
                string url = dde.Request("URL", int.MaxValue);
                string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
                dde.Disconnect();
                return text[0].Substring(1);
            } catch {
                return null;
            }
        }

        public IList<FileRecordItem> GetWindowExplorerFilePath() {

            var listPaths = new List<FileRecordItem>();
            SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

            string filename;

            foreach (SHDocVw.InternetExplorer ie in shellWindows) {
                filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

                if (filename.Equals("explorer")) {
                    // Save the location off to your application
                    Console.WriteLine("Explorer location : {0}", ie.LocationURL);
                    listPaths.Add(new FileRecordItem { 
                        FileName = ie.LocationName,
                        FilePath = HttpUtility.UrlDecode(ie.LocationURL.Replace(@"file:///", ""))
                    });
                  
                    // Setup a trigger for when the user navigates
                   // ie.NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(handlerMethod);
                }
            }

            return listPaths;
        }
    }
}
